package com.uni.sofia.reader.utils
{
	import com.uni.sofia.reader.magazine.Magazine;
	import com.uni.sofia.reader.magazine.Page;
	
	import mx.controls.DateField;
	import mx.controls.Image;

	/**
    * Utility class for creating and managing magazines
    * 
    */
	public class MagazineBuilder
	{
		/**
		 * Loads a magazine from given xml magazine representation
		 * 
		 * @param xml
		 * 		the xml from whcich will be build <code>Magazine</code> object
		 * 
		 * @return newly created magazine, or null if the xml is invalid
		 */ 
		public static function loadMagazine(xml:XML):Magazine{
			if(xml == null) return null;
			
			var magazine:Magazine = new Magazine();
			magazine.name = xml.name;
			magazine.publisher = xml.publisher;
			var year:String = xml.year;
			var month:String = xml.month;
			var day:String = xml.day;
			magazine.released = new Date(year, month, day);
			
			for each (var page:XML in xml.pages.page)
			{
				magazine.addPage(new Page(page.number, page.description, page.image));
			}
			
			magazine.sortPages();
			return magazine;
		}
	}
}